package com.lineage.server.datatables.sql;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.storage.CharItemsPowerStorage;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.templates.L1ItemPower_name;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.WorldItem;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CharItemsPowerTable implements CharItemsPowerStorage {
  private static final Log _log = LogFactory.getLog(BuddyTable.class);
  
  private static final Map<Integer, L1ItemPower_name> _powerMap = new HashMap<>();
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    int i = 0;
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `character_item_power`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int item_obj_id = rs.getInt("item_obj_id");
        int hole_count = rs.getInt("hole_count");
        int hole_1 = rs.getInt("hole_1");
        int hole_2 = rs.getInt("hole_2");
        int hole_3 = rs.getInt("hole_3");
        int hole_4 = rs.getInt("hole_4");
        int hole_5 = rs.getInt("hole_5");
        int item_power1 = rs.getInt("item_power1");
        int item_power2 = rs.getInt("item_power2");
        int item_power3 = rs.getInt("item_power3");
        int item_power4 = rs.getInt("item_power4");
        L1ItemPower_name power = new L1ItemPower_name();
        power.set_item_obj_id(item_obj_id);
        power.set_hole_count(hole_count);
        power.set_hole_1(hole_1);
        power.set_hole_2(hole_2);
        power.set_hole_3(hole_3);
        power.set_hole_4(hole_4);
        power.set_hole_5(hole_5);
        addValue(item_obj_id, power);
        i++;
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    _log.info("载入物件凹槽清单资料数量: " + i + "(" + timer.get() + "ms)");
  }
  
  private static void addValue(int item_obj_id, L1ItemPower_name power) {
    L1ItemInstance item = WorldItem.get().getItem(Integer.valueOf(item_obj_id));
    boolean isError = true;
    if (item != null) {
      if (item.get_power_name() == null)
        item.set_power_name(power); 
      _powerMap.put(Integer.valueOf(item_obj_id), power);
      isError = false;
    } 
  }
  
  private static void errorItem(int item_obj_id) {
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("DELETE FROM `character_item_power` WHERE `item_obj_id`=?");
      pstm.setInt(1, item_obj_id);
      pstm.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public void delItem(int item_obj_id) {
    if (_powerMap.get(Integer.valueOf(item_obj_id)) != null)
      _powerMap.remove(new Integer(item_obj_id)); 
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("DELETE FROM `character_item_power` WHERE `item_obj_id`=?");
      pstm.setInt(1, item_obj_id);
      pstm.execute();
    } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) {
    
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public void storeItem(int item_obj_id, L1ItemPower_name power) throws Exception {
    if (_powerMap.get(Integer.valueOf(item_obj_id)) != null)
      return; 
    _powerMap.put(Integer.valueOf(item_obj_id), power);
    Connection con = null;
    PreparedStatement pstm = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement(
          "INSERT INTO `character_item_power` SET `item_obj_id`=?,`hole_count`=?,`hole_1`=?,`hole_2`=?,`hole_3`=?,`hole_4`=?,`hole_5`=?");
      int i = 0;
      pstm.setInt(++i, item_obj_id);
      pstm.setInt(++i, power.get_hole_count());
      pstm.setInt(++i, power.get_hole_1());
      pstm.setInt(++i, power.get_hole_2());
      pstm.setInt(++i, power.get_hole_3());
      pstm.setInt(++i, power.get_hole_4());
      pstm.setInt(++i, power.get_hole_5());
      pstm.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public void updateItem(int item_obj_id, L1ItemPower_name power) {
    Connection co = null;
    PreparedStatement pm = null;
    try {
      co = DatabaseFactory.get().getConnection();
      pm = co.prepareStatement(
          "UPDATE `character_item_power` SET `hole_count`=?,`hole_1`=?,`hole_2`=?,`hole_3`=?,`hole_4`=?,`hole_5`=? WHERE `item_obj_id`=?");
      int i = 0;
      pm.setInt(++i, power.get_hole_count());
      pm.setInt(++i, power.get_hole_1());
      pm.setInt(++i, power.get_hole_2());
      pm.setInt(++i, power.get_hole_3());
      pm.setInt(++i, power.get_hole_4());
      pm.setInt(++i, power.get_hole_5());
      pm.setInt(++i, item_obj_id);
      pm.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(pm);
      SQLUtil.close(co);
    } 
  }
  
  public Map<Integer, L1ItemPower_name> getPowerMap() {
    return _powerMap;
  }
}
